package com.ccsu.mymarket.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ccsu.mymarket.entity.OrderInfo;
import com.ccsu.mymarket.entity.message.Meta;
import com.ccsu.mymarket.entity.message.Result;
import com.ccsu.mymarket.exception.MyException;
import com.ccsu.mymarket.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

//@UserLoginToken
@RestController
@RequestMapping("/orders")
public class OrderController {
    private final OrderService orderService;

    public OrderController(@Autowired OrderService orderService) {
        this.orderService = orderService;
    }

    @PostMapping("/add/batch")
//    购物车下单入口,传入 用户id 和 书籍id列表
    public Result addOrder(@RequestBody Map data) throws MyException {
        if (data == null )
        {
            throw new MyException("购物车下单失败,传入的信息为空！");
        }
        else {
            Integer id= (Integer) data.get("userId");
            List<Integer> bookIds= (List<Integer>) data.get("bookIds");

            if (orderService.batchAddOrder(bookIds,id))
            return new Result(new Meta(200, "下单成功！"), null);
        }
        return new Result(new Meta(500, "内部错误！"), null);
    }

    /**
     * @param order
     *  发货，收货 时，传入订单id + 订单新的状态
     *  评论时 ，传入订单id + 订单新的状态 + 评论内容
     * @return
     */
    @PutMapping("/update")
    public Result updateOrder(@RequestBody OrderInfo order) {
        if (order == null)
            return new Result(new Meta(403, "请输入修改数据！"), null);
        else {
            return orderService.updateOrder(order);
        }
    }

    @GetMapping("/select/{id}")
//    传入用户id
    public Result getOrderByUserId(@PathVariable Integer id) {
        List<OrderInfo> orderInfoList = orderService.getOrderByUserId(id);
        if (orderInfoList == null)
            return new Result(new Meta(201, "暂无数据"), null);
        return new Result(new Meta(200, "查询成功！"), orderInfoList);
    }

    @GetMapping("/comment/{id}")
//    传入商品id获取相关评论
//    返回格式,comment-评论内容; commentTime-评论时间
    public Result getCommentByBookId(@PathVariable Integer id) {
        List<Map> commentList = orderService.getComments(id);
        if (commentList == null)
            return new Result(new Meta(201, "暂无数据"), commentList);
        return new Result(new Meta(200, "查询成功！"), commentList);
    }

    @DeleteMapping("/delete/{id}")
//    传入订单id
    public Result deleteOrder(Integer id){
        Integer state=orderService.getOne(new LambdaQueryWrapper<OrderInfo>()
                .eq(OrderInfo::getId,id).select(OrderInfo::getState)).getState();
        if (state != -1 && state != 3)
            return new Result(new Meta(403, "该订单状态不允许进行删除操作！"), null);
        else {
            if (orderService.removeById(id))
                return new Result(new Meta(200, "删除成功！"), null);
        }
        return new Result(new Meta(500, "操作失败！"), null);
    }
}
